<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BMessageContext
 *
 * @author jianfeng
 */
class BMessageContext {

    //put your code here
    public $name = 'no-name';
    public $allow_from = array();
    public $allow_to = array();
    public $senderPermits = array();
    public $receiverPermits = array();
    public $messageTypes = array();

    public function checkSendPermit($sender, $message = null, $data = NULL) {

        //check message permit
        $checkMessage = TRUE;
        foreach ($this->messageTypes as $className) {
            if ($message instanceof $className)
                $checkMessage = TRUE;
        }

        $checkAddress = FALSE;
        //allow_to format, array('space'=>'id or name', 'app'=>'id or name',...)
        /* address format is same as all_to format,if set value in address
         * then will check to make sure it is allowed address, if not set
         * do not check
         */
        if (is_array($sender))
            $sendAddress = BAddress::buildFromArray($sender);
        if (is_string($sender))
            $sendAddress = BAddress::buildFromString($sender);
        if ($sender instanceof BAddress)
            $sendAddress = $sender;

        if ($sendAddress instanceof BAddress) {
            $sendAddress->filterByContext($this->allow_to);
            if (count($sendAddress->getAddressItems()) > 0)
                $checkAddress = TRUE;
        }

        if ($checkAddress && $checkMessage)
            return TRUE;

        //by default, return false
        return FALSE;
    }

    public function checkReceivePermit($receiver, $message = null, $data = NULL) {
        //check message permit
        $checkMessage = TRUE;
        foreach ($this->messageTypes as $className) {
            if ($message instanceof $className)
                $checkMessage = TRUE;
        }

        $checkAddress = FALSE;
        //allow_to format, array('space'=>'id or name', 'app'=>'id or name',...)
        /* address format is same as all_to format,if set value in address
         * then will check to make sure it is allowed address, if not set
         * do not check
         */
        if (is_array($receiver))
            $receiverAddress = BAddress::buildFromArray($receiver);
        if (is_string($receiver))
            $receiverAddress = BAddress::buildFromString($receiver);
        if ($receiver instanceof BAddress)
            $receiverAddress = $receiver;

        if ($receiverAddress instanceof BAddress) {
            $receiverAddress->filterByContext($this->allow_from);
            if (count($receiverAddress->getAddressItems()) > 0)
                $checkAddress = TRUE;
        }

        if ($checkAddress && $checkMessage)
            return TRUE;

        //by default, return false
        return FALSE;
    }

    public static function buildFromArray($context) {

        $oContext = new BMessageContext();

        if (is_array($context) && $context != NULL) {
            if (isset($context['sender']) && $context['sender'] != NULL) {
                $oContext->senderPermits = $context['sender'];
            }
            if (isset($context['receiver']) && $context['receiver'] != NULL) {
                $oContext->receiverPermits = $context['receiver'];
            }
            if (isset($context['message_from']) && $context['message_from'] != NULL) {
                $oContext->allow_from = $context['message_from'];
            }
            if (isset($context['allow_from']) && $context['allow_from'] != NULL) {
                $oContext->allow_from = $context['allow_from'];
            }
            if (isset($context['message_to']) && $context['message_to'] != NULL) {
                $oContext->allow_to = $context['message_from'];
            }
            if (isset($context['allow_to']) && $context['allow_to'] != NULL) {
                $oContext->allow_to = $context['allow_to'];
            }
            if (isset($context['message']) && $context['message'] != NULL) {
                array_push($oContext->messageTypes, array('class' => $context['message']));
            }
        }

        return $oContext;
    }

    protected function refactorVariables() {
        
    }

}
